package com.baractivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import com.barcode.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MyCamera extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cameraphoto);
		
		Button btn=(Button)findViewById(R.id.btn_photograph1);
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				
				startActivityForResult(intent, 0);
			}
		});
	
		Button btnback = (Button) this.findViewById(R.id.btn_back);
          btnback.setOnClickListener(new OnClickListener(){
          	
          	public void onClick(View v){
          		Intent intent=new Intent(MyCamera.this,CaptureActivity.class);
          		startActivity(intent);
          		
          	}
          	
          });
				
	
	}
		 @Override
		    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		        super.onActivityResult(requestCode, resultCode, data);

		        if (resultCode == Activity.RESULT_OK) {

		            String sdStatus = Environment.getExternalStorageState();
		            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检查sd卡
		                Log.v("TestFile",
		                        "SD card is not avaiable/writeable right now.");
		                return;
		            }

		            Bundle bundle = data.getExtras();
		            Bitmap bitmap = (Bitmap) bundle.get("data");//获取数据bitmap
		            FileOutputStream b = null;
		           
		            File file = new File("/sdcard/myImage/");
		            if(!file.exists()){
		            	file.mkdirs();
		            }
		            new DateFormat();
					String name=DateFormat.format("yyyyMMdd_hhmmss",
		            		Calendar.getInstance(Locale.CHINA))	+ ".png";;
		            String fileName = "sdcard/myImage/" + name ;
						   
		            try {
		                b = new FileOutputStream(fileName);
		                bitmap.compress(Bitmap.CompressFormat.PNG, 100, b);
		                Toast.makeText(this, "保存在/sdcard/myImage/", Toast.LENGTH_LONG).show();
		            } catch (FileNotFoundException e) {
		                e.printStackTrace();
		            } finally {
		                try {
		                    b.flush();
		                    b.close();
		                } catch (IOException e) {
		                    e.printStackTrace();
		                }
		            }
		            ((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bitmap);
		          
		        }
		    }		
}
	




